package com.qqt.csr.message.event.intrasite;

import com.qqt.csr.message.event.EventPublisherStrategy;
import com.qqt.csr.message.event.EventPublisherStrategyFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

/**
 * @Author: yang hao
 * @Date: 2024/12/11 13:41
 * @Description: 站内信事件发布者
 **/
@Slf4j
@Component
public class IntraSiteMessageEventPublisher {

    private final ApplicationEventPublisher eventPublisher;

    private final EventPublisherStrategyFactory strategyFactory;

    /**
     * 构造函数
     * @param eventPublisher {@link ApplicationEventPublisher} 事件发布者
     * @param strategyFactory {@link EventPublisherStrategyFactory} 事件发布策略工厂
     */
    public IntraSiteMessageEventPublisher(ApplicationEventPublisher eventPublisher, EventPublisherStrategyFactory strategyFactory) {
        this.eventPublisher = eventPublisher;
        this.strategyFactory = strategyFactory;
    }

    /**
     * 发布站内信事件
     * @param source {@link Object} 事件源
     */
    public void publishEvent(Object source) {
        EventPublisherStrategy strategy = strategyFactory.getStrategy(source);
        if (strategy != null) {
            strategy.publish(eventPublisher, source);
        } else {
            log.warn("No strategy found for source type: {}", source.getClass().getName());
        }
    }

}
